package com.yanqu.road.dao.impl.channelcenter.washe;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.washe.data.WasheUserJinBiaoData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WasheUserJinBiaoDataDaoImpl extends TempDataObjectDao<WasheUserJinBiaoData> {

    public static String getWasheUserJinBiaoDataSql = "select * from `t_u_washe_user_jinbiao` where `userId`=?";
    public WasheUserJinBiaoData getWasheUserJinBiaoData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getWasheUserJinBiaoDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateWasheUserJinBiaoDataSql = "update `t_u_washe_user_jinbiao` set dataSeason=?,score=?,energy=?,continueWinTimes=?,dayResetTime=?,peakWinTimes=?,peakScore=?,hadRewardStageList=?,seasonScoreMap=?,seasonTopScoreMap=? where userId=?";
    @Override
    protected boolean updateData(WasheUserJinBiaoData washeUserJinBiaoData) {
        washeUserJinBiaoData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, washeUserJinBiaoData.getDataSeason()));
        para.put(i++, new DbParameter(Types.BIGINT, washeUserJinBiaoData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, washeUserJinBiaoData.getEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, washeUserJinBiaoData.getContinueWinTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, washeUserJinBiaoData.getDayResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, washeUserJinBiaoData.getPeakWinTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, washeUserJinBiaoData.getPeakScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeUserJinBiaoData.getHadRewardStageList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeUserJinBiaoData.getSeasonScoreMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeUserJinBiaoData.getSeasonTopScoreMap())));
        para.put(i++, new DbParameter(Types.BIGINT, washeUserJinBiaoData.getUserId()));
        if (getDb().executeNoneQuery(updateWasheUserJinBiaoDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertWasheUserJinBiaoDataSql = "insert into `t_u_washe_user_jinbiao`(userId,dataSeason,score,energy,continueWinTimes,dayResetTime,peakWinTimes,peakScore,hadRewardStageList,seasonScoreMap,seasonTopScoreMap) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(WasheUserJinBiaoData washeUserJinBiaoData) {
        washeUserJinBiaoData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, washeUserJinBiaoData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, washeUserJinBiaoData.getDataSeason()));
        para.put(i++, new DbParameter(Types.BIGINT, washeUserJinBiaoData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, washeUserJinBiaoData.getEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, washeUserJinBiaoData.getContinueWinTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, washeUserJinBiaoData.getDayResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, washeUserJinBiaoData.getPeakWinTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, washeUserJinBiaoData.getPeakScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeUserJinBiaoData.getHadRewardStageList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeUserJinBiaoData.getSeasonScoreMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeUserJinBiaoData.getSeasonTopScoreMap())));
        if (getDb().executeNoneQuery(insertWasheUserJinBiaoDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected WasheUserJinBiaoData parseData(ResultSet rs) throws SQLException {
        WasheUserJinBiaoData washeUserJinBiaoData = new WasheUserJinBiaoData();
        washeUserJinBiaoData.setUserId(rs.getLong("userId"));
        washeUserJinBiaoData.setDataSeason(rs.getInt("dataSeason"));
        washeUserJinBiaoData.setScore(rs.getLong("score"));
        washeUserJinBiaoData.setEnergy(rs.getInt("energy"));
        washeUserJinBiaoData.setContinueWinTimes(rs.getInt("continueWinTimes"));
        washeUserJinBiaoData.setDayResetTime(rs.getLong("dayResetTime"));
        washeUserJinBiaoData.setPeakWinTimes(rs.getInt("peakWinTimes"));
        washeUserJinBiaoData.setPeakScore(rs.getLong("peakScore"));
        String hadRewardStageListStr = rs.getString("hadRewardStageList");
        List<Integer> hadRewardStageList = new ArrayList<>();
        if(!StringUtils.isNullOrEmpty(hadRewardStageListStr)){
            hadRewardStageList = JSON.parseObject(hadRewardStageListStr, new TypeReference<List<Integer>>() {
            });
        }
        washeUserJinBiaoData.setHadRewardStageList(hadRewardStageList);

        String seasonScoreMap = rs.getString("seasonScoreMap");
        if (!StringUtils.isNullOrEmpty(seasonScoreMap)) {
            washeUserJinBiaoData.setSeasonScoreMap(JSON.parseObject(seasonScoreMap, new TypeReference<ConcurrentHashMap<Integer, Long>>() {
            }));
        }

        String seasonTopScoreMap = rs.getString("seasonTopScoreMap");
        if (!StringUtils.isNullOrEmpty(seasonTopScoreMap)) {
            washeUserJinBiaoData.setSeasonTopScoreMap(JSON.parseObject(seasonTopScoreMap, new TypeReference<ConcurrentHashMap<Integer, Long>>() {
            }));
        }

        return washeUserJinBiaoData;
    }

}
